package com.arm.controller.api.admin.user;

import com.arm.controller.BaseController;
import com.arm.def.Constant;
import com.arm.def.MessageConstant;
import com.arm.module.AdminUser;
import com.arm.service.AdminUserService;
import com.arm.service.MenuService;
import com.arm.service.RoleService;
import com.arm.tools.MD5Tools;
import com.arm.tools.myRunTime.MyRunTimeExcption;
import com.arm.tools.result.ResultObj;
import com.arm.tools.result.ReturnCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Andrew  on 2017/2/26.
 */
@Api(description = "后台用户接口")
@RestController
@RequestMapping(value = "/api/admin/user")
public class UserController extends BaseController {
    /*@Autowired
    RedisCache redisCache;*/
    @Autowired
    RoleService roleService;
    @Autowired
    MenuService menuService;
    @Autowired
    AdminUserService adminUserService;


    @ApiOperation(value = "获取用户列表", notes = "获取用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", defaultValue = "1",required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "10",required = false, paramType = "query", dataType = "int")
    })
    @GetMapping(value = "/list")
    public ResultObj<PageInfo<AdminUser>> getUserList(Integer page, Integer pageSize) {
        ResultObj resultObj = new ResultObj();
        try {

            if (page == null || page == 0) {
                page = Constant.PAGENUM;
            }
            // 设置每页记录数
            if (pageSize == null || pageSize == 0) {
                pageSize = Constant.PAGESIZENum;
            }
            PageHelper.startPage(page, pageSize);

            List<AdminUser> userList = adminUserService.getUserList(new HashMap<String, String>() {{
                put("userName", "");
                put("realName", "");
            }});

            PageInfo<AdminUser> pageinfo = new PageInfo<AdminUser>(userList);
            resultObj.setData(pageinfo);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }

        return resultObj;

    }

    @ApiOperation(value = "获取用户", notes = "获取用户")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "tid", value = "用户Id", required = true, paramType = "path", dataType = "long")
    })
    @GetMapping(value = "/{tid}")
    public ResultObj<AdminUser> getUser(@PathVariable("tid") Long tid) {

        ResultObj<AdminUser> resultObj = new ResultObj();
        try {
            AdminUser adminUser = adminUserService.getUser(new HashMap<String, String>() {
                {
                    put("tid", String.valueOf(tid));
                }
            });
            resultObj.setData(adminUser);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }

        return resultObj;
    }

    /**
     * 修改后台用户信息
     *
     * @return
     */
    @ApiOperation(value = "修改后台用户信息", notes = "修改后台用户信息")
    @ApiImplicitParam(name = "adminUser", value = "用户详细实体user", required = true, dataType = "AdminUser")
    @PutMapping(value = "/modifyUser")
    public ResultObj modifyUser(@RequestBody AdminUser adminUser) {

        ResultObj resultObj = new ResultObj();
        try {
            adminUserService.modifyUser(adminUser);
            resultObj.setMsg("成功！");
        } catch (MyRunTimeExcption myRunTimeExcption) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj;
    }


    @ApiOperation(value = "修改后台用户密码", notes = "修改后台用户密码")
    @PutMapping(value = "/modifyPassword")
    public ResultObj modifyPassword(String oldPassWord, String passWord) {
        ResultObj resultObj = new ResultObj();
        try {
            AdminUser adminUser = (AdminUser) super.session.getAttribute(Constant.SESSION_KEY_ADMIN_USER_LOGIN);
            if (adminUser == null) {
                throw new MyRunTimeExcption(MessageConstant.USER_INEXISTENCE);
            }
            String pageOldPassWord = MD5Tools.MD5(oldPassWord);

            if (!pageOldPassWord.equals(adminUser.getPassWord())) {
                logger.info(pageOldPassWord + "===>>>" + adminUser.getPassWord());
                throw new MyRunTimeExcption(MessageConstant.PASSWORD_ERRO);
            }
            adminUser.setPassWord(MD5Tools.MD5(passWord));
            adminUserService.modifyUser(adminUser);
            resultObj.setMsg(MessageConstant.MODIFY_PASSWORD_SUCCESS);

        } catch (MyRunTimeExcption myRunTimeExcption) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return resultObj;

    }

    @ApiOperation(value = "重置后台用户密码", notes = "重置后台用户密码")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "tid", value = "用户Id", required = true, paramType = "path", dataType = "long")
    })
    @PutMapping(value = "/{tid}")
    public ResultObj resetPassword(@PathVariable("tid") Long tid) {
        System.out.println("=================================resetPassword==========================================>>>>>>");

        ResultObj resultObj = new ResultObj();
        try {
            AdminUser adminUser = adminUserService.selectAdminById(new HashMap<String, String>() {{
                put("tid", String.valueOf(tid));
            }});
            if (adminUser == null) {
                throw new MyRunTimeExcption(MessageConstant.USER_INEXISTENCE);
            }
            adminUser.setPassWord(MD5Tools.MD5("123456"));
            adminUserService.modifyUser(adminUser);
            resultObj.setMsg(MessageConstant.PASSWORD_RESET_SUCCESS);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return resultObj;

    }

}
